package cn.chenc.framework.jpa.model;

import cn.chenc.framework.core.text.Convert;
import org.springframework.data.domain.Page;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 　@description: TODO
 * 　@author secret
 * 　@date 2021/6/5 23:10
 *
 */
public class PageInfo implements Serializable {
    private static final long serialVersionUID = 1L;
    private int pageNum = 0;
    private int pageSize = 20;
    private boolean first = false;
    private boolean last = false;
    private int totalPage;
    private int totalCount;
    private int currPageCount;
    private List<?> content = Collections.emptyList();

    public PageInfo(int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public static PageInfo of(int pageNum, int pageSize) {
        return new PageInfo(pageNum, pageSize);
    }

    public PageInfo(int pageNum, int pageSize, int totalCount) {
        this.pageNum = pageNum == 0 ? 1 : pageNum;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        this.first = pageNum == 0 || pageNum == 1;
        this.last = pageNum == this.totalPage;
    }

    public PageInfo() {
    }

    public static int offsetCurrent(int pageNum, int pageSize) {
        return pageNum > 0 ? (pageNum - 1) * pageSize : 0;
    }

    public static PageInfo convertPageable(Page<?> pageable) {
        PageInfo pageUtil = new PageInfo();
        pageUtil.setPageNum(pageable.getNumber() == 0 ? 1 : pageable.getNumber());
        pageUtil.setPageSize(pageable.getSize());
        pageUtil.setContent(pageable.getContent());
        pageUtil.setFirst(pageable.isFirst());
        pageUtil.setLast(pageable.isLast());
        pageUtil.setCurrPageCount(pageable.getNumberOfElements());
        pageUtil.setTotalCount(Convert.toInt(pageable.getTotalElements()));
        pageUtil.setTotalPage(pageable.getTotalPages());
        return pageUtil;
    }

    public static Integer getFirstResult(Integer pageNumber, Integer pageSize) {
        return pageNumber > 0 ? pageSize * (pageNumber - 1) : 0;
    }

    public int getPageNum() {
        return this.pageNum;
    }

    public int getPageSize() {
        return this.pageSize;
    }

    public boolean isFirst() {
        return this.first;
    }

    public boolean isLast() {
        return this.last;
    }

    public int getTotalPage() {
        return this.totalPage;
    }

    public int getTotalCount() {
        return this.totalCount;
    }

    public int getCurrPageCount() {
        return this.currPageCount;
    }

    public List<?> getContent() {
        return this.content;
    }

    public void setPageNum(final int pageNum) {
        this.pageNum = pageNum;
    }

    public void setPageSize(final int pageSize) {
        this.pageSize = pageSize;
    }

    public void setFirst(final boolean first) {
        this.first = first;
    }

    public void setLast(final boolean last) {
        this.last = last;
    }

    public void setTotalPage(final int totalPage) {
        this.totalPage = totalPage;
    }

    public void setTotalCount(final int totalCount) {
        this.totalCount = totalCount;
    }

    public void setCurrPageCount(final int currPageCount) {
        this.currPageCount = currPageCount;
    }

    public void setContent(final List<?> content) {
        this.content = content;
    }
}
